import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import 'package:get/get_state_manager/get_state_manager.dart';
import 'package:smart_lam/dict/dict.dart';
import 'package:smart_lam/style/style.dart';
import 'package:smart_lam/widget/BlackBg.dart';

import 'ble_devices_controller.dart';

class BleDevicesPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    Widget svgItem(int v) {
      late int result;
      if (v <= -100) {
        result = 0;
      } else if (v > -100 && v <= -88) {
        result = 1;
      } else if (v > -88 && v <= -77) {
        result = 2;
      } else if (v > -77 && v <= -55) {
        result = 3;
      } else if (v > -55) {
        result = 4;
      }
      return SvgPicture.asset(
        'assets/svg/signal_$result.svg',
        height: ScreenUtil().setHeight(20),
      );
    }

    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.black,
        leading: IconButton(
          icon: Icon(
            Icons.keyboard_backspace_outlined,
            color: Colors.white,
            size: ScreenUtil().setSp(24),
          ),
          onPressed: () {
            Get.back<void>();
          },
        ),
        automaticallyImplyLeading: false,
      ),
      body: GetBuilder<BleDevicesController>(
        builder: (_) {
          return Stack(
            clipBehavior: Clip.antiAliasWithSaveLayer,
            children: [
              BlackBg(),
              SingleChildScrollView(
                child: ListTileTheme(
                  dense: true,
                  child: Column(
                    children: _.devicesWithState
                        .map(
                          (e) => Column(
                            children: [
                              ListTile(
                                title: Text(
                                  e.discoveredDevice.name == '' ? '---' : e.discoveredDevice.name,
                                  style: CustomStyle(context).customTextStyle(
                                      fontSize: 14, color: Colors.white),
                                ),
                                subtitle: Container(
                                  padding: EdgeInsets.symmetric(
                                      vertical: ScreenUtil().setWidth(10)),
                                  child: Row(
                                    crossAxisAlignment: CrossAxisAlignment.end,
                                    children: [
                                      Text(
                                        '信号强度：',
                                        textAlign: TextAlign.end,
                                        style: TextStyle(
                                          fontSize: ScreenUtil().setSp(14),
                                        ),
                                      ),
                                      Padding(
                                          padding: EdgeInsets.only(
                                              bottom: ScreenUtil().setHeight(4)),
                                          child: svgItem(e.discoveredDevice.rssi))
                                    ],
                                  ),
                                ),
                                trailing: OutlinedButton(
                                  onPressed: () {
                                    _.connect(e.discoveredDevice);
                                  },
                                  child: Text(
                                    Dict.getByNameKey(deviceStateMap, e.connectionState)??'',
                                    style: TextStyle(color: Colors.white),
                                  ),
                                ),
                              ),
                              Divider(
                                indent: ScreenUtil().setWidth(16),
                                endIndent: ScreenUtil().setWidth(16),
                                color: CustomStyle.lineColor,
                              )
                            ],
                          ),
                        )
                        .toList(),
                  ),
                ),
              ),
            ],
          );
        },
      ),
    );
  }
}
